package com.qf.shopresources.controller;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fileUpload")
public class FileUpLoadController {
    @Value("${fdfs.path}")
    private String nginxPath;
    @Autowired
    private FastFileStorageClient client;

    @RequestMapping("/upload")
    public String upload(MultipartFile file) {

        try {
            //获取文件的后缀名
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            //上传图片和缩略图
            StorePath storePath = client.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), extension, null);
            //获取文件的路径
            String fullPath = storePath.getFullPath();
            //通过nginx获得绝对路径
            return nginxPath + fullPath;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

}
